package jumpingnotes.dao.hibernate;

import java.util.List;

import jumpingnotes.dao.DeployDao;
import jumpingnotes.model.entity.Deploy;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class DeployHibernateDao extends GenericHibernateDao<Deploy> implements DeployDao
{
	public DeployHibernateDao()
	{
		super(Deploy.class);
	}

	@SuppressWarnings("unchecked")
	public Deploy findDeployByUuid(final String uuid)
	{
		return (Deploy) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Query query = session.createQuery("select distinct d from Deploy d where d.uuid=:uuid");
				query.setString("uuid", uuid);
				return query.uniqueResult();
			}
		});
	}

	@SuppressWarnings("unchecked")
	public List<Deploy> findDeployByAudioId(final int audioId, final int firstResult, final int maxResults)
	{
		return (List<Deploy>) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Query query = session.createQuery("select distinct d from Deploy d where d.audio.audioId=:audioId");
				query.setInteger("audioId", audioId);

				if (firstResult >= 0)
					query.setFirstResult(firstResult);
				if (maxResults > 0)
					query.setMaxResults(maxResults);

				return query.list();
			}
		});
	}
}
